<template>
  <page-header-wrapper :title="false">
    <a-card>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="门店">
                <a-select v-decorator="queryParam.storeId" placeholder="请选择门店">
                  <a-select-option :value="i.id" v-for="i in storeOption" :key="i.id">{{
                    i.storeName
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="周期">
                <a-select v-decorator="queryParam.date" placeholder="请选择周期" @change="getCurDates" defaultValue="1">
                  <a-select-option value="1">近7天</a-select-option>
                  <a-select-option value="2">近15天</a-select-option>
                  <a-select-option value="3">近30天</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="24">
              <a-form-item label="品牌">
                <a-select v-decorator="['brand']" placeholder="请选择品牌">
                  <a-select-option :value="i.id" v-for="i in brandOption" :key="i.id">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <a-col :md="6" :sm="24">
              <a-form-item label="分类">
                <a-select v-decorator="queryParam.gdClsId" placeholder="请选择分类">
                  <a-select-option :value="i.id" v-for="i in option" :key="i.id">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 6) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" :loading="loading" @click="handleSearch(queryParam)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-table ref="table" size="default" rowKey="key" :columns="columns" :data-source="loadData">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="status" slot-scope="text, record">
          <template>
            <a-tag color="#82af6f" v-if="record?.status == 1">已审核</a-tag>
            <a-tag color="#D15B47" v-else>待审核</a-tag>
          </template>
        </span>
        <span slot="action" slot-scope="text, record">
          <template> <a-tag color="#428bca" @click="handleInDetail(record)">入库详情</a-tag> </template>
          <template> <a-tag color="#428bca" @click="handleOutDetail(record)">退库详情</a-tag> </template>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { omit } from 'lodash-es'
import { getGdBrandListUsingPOST } from '@/api/api/shangpinguanli.ts'
import { getGdClassListUsingPOST } from '@/api/api/shangpinguanli.ts'
import { getStoreListUsingPOST } from '@/api/api/mendianguanli.ts'
import { getTurnoverListUsingPOST } from '@/api/api/tongjibaobiao.ts'

export default {
  name: 'PurchasingReportGoodsChannel',
  data() {
    return {
      brandOption: [],
      option: [],
      storeOption: [],
      mdl: null,
      visible: false,
      activeTabKey: '1',
      confirmLoading: false,
      loading: false,
      pagination: {
        total: 0,
      },
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        date: [moment().startOf('months').format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')],
        gdClsId: '',
        queryCondition: '',
        status: '',
      },
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      // 表头
      columns: [
        {
          title: '品牌',
          dataIndex: 'name',
        },
        {
          title: '库存',
          dataIndex: 'invNum',
        },
        {
          title: '库存占比',
          dataIndex: 'invPercent',
        },
        {
          title: '库存成本',
          dataIndex: 'invCost',
        },
        {
          title: '库存成本占比',
          dataIndex: 'invCostPercent',
        },
        {
          title: '销量',
          dataIndex: 'salesNum',
        },
        {
          title: '销量占比',
          dataIndex: 'salesPercent',
        },
        {
          title: '销售额',
          dataIndex: 'amount',
        },
        {
          title: '销售额占比',
          dataIndex: 'amountPercent',
        },
        {
          title: '毛利',
          dataIndex: 'grossProfit',
        },
        {
          title: '毛利占比',
          dataIndex: 'grossProfitPercent',
        },
        {
          title: '毛利率',
          dataIndex: 'grossMargin',
        },
        {
          title: '单毛',
          dataIndex: 'avgGrossProfit',
        },
        {
          title: '周转天数',
          dataIndex: 'turnoverDay',
        },
      ],

      // 加载数据方法 必须为 Promise 对象
      loadData: [],
    }
  },
  mounted() {
    this.getOption()
    this.getClassOption()
    this.getStoreOption()
    this.handleSearch(this.queryParam)
  },
  methods: {
    getCurDates(val) {
      switch (val) {
        case '1':
          this.queryParam.date[0] = moment().subtract(7, 'days').format('YYYY-MM-DD')
          break
        case '2':
          this.queryParam.date[0] = moment().subtract(15, 'days').format('YYYY-MM-DD')
          break
        case '3':
          this.queryParam.date[0] = moment().subtract(30, 'days').format('YYYY-MM-DD')
          break
      }
      this.queryParam.date[1] = moment().format('YYYY-MM-DD')
      console.log(this.queryParam.date)
    },

    getClassOption() {
      getGdClassListUsingPOST(this.optionParam)
        .then((res) => {
          this.option = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    getOption() {
      getGdBrandListUsingPOST(this.optionParam)
        .then((res) => {
          this.brandOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    getStoreOption() {
      getStoreListUsingPOST(this.optionParam)
        .then((res) => {
          this.storeOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleSearch(val) {
      this.loading = true
      const params = {
        ...val,
        startTime: val?.date?.[0],
        endTime: val?.date?.[1],
      }
      getTurnoverListUsingPOST({ ...omit(params, ['date']) })
        .then((res) => {
          this.loadData = res.data.list
          this.loading = false
          this.queryParam.pageNo = this.pagination.current
          this.pagination = { ...this.pagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.loading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleInDetail(record) {
      this.$router.push({ path: `/report/purchasingReport/goodsInStoreDetail/${record?.id}` })
    },
    handleOutDetail(record) {
      this.$router.push({ path: `/report/purchasingReport/goodsOutStoreDetail/${record?.id}` })
    },
  },
}
</script>
